package GUI;

import beans.User;
import service.CustomerService;
import service.MerchantService;
import service.ProducerService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class loginFrameN extends JFrame {
    JFrame loginFrameN = new JFrame("用户名登录");
    JLabel nameLabel = new JLabel("用户名：");
    JLabel passwordLabel = new JLabel("密    码：");
    JTextField nameField = new JTextField(20);
    JTextField passwordField = new JTextField(20);
    JButton loginButtonC = new JButton("顾客登录");
    JButton loginButtonM = new JButton("商家登录");
    JButton loginButtonP = new JButton("生产商登录");
    JButton returnButton = new JButton("返回");
    JPanel namePanel = new JPanel();
    JPanel passwordPanel = new JPanel();
    JPanel buttonPanel = new JPanel();
    
    public void loginFrameN(){
        namePanel.add(nameLabel);
        namePanel.add(nameField);
        passwordPanel.add(passwordLabel);        
        passwordPanel.add(passwordField);
        buttonPanel.add(loginButtonC);
        buttonPanel.add(loginButtonM);
        buttonPanel.add(loginButtonP);
        buttonPanel.add(returnButton);

        loginFrameN.add(namePanel, BorderLayout.NORTH);
        loginFrameN.add(passwordPanel,BorderLayout.CENTER);
        loginFrameN.add(buttonPanel,BorderLayout.SOUTH);
        loginFrameN.setSize(500,200);
        loginFrameN.setLocationRelativeTo(null);
        loginFrameN.setVisible(true);
        loginFrameN.setDefaultCloseOperation(2);

        //顾客登录
        loginButtonC.addActionListener(e -> {
            String name = nameField.getText();
            String password = passwordField.getText();

            CustomerService cs = new CustomerService();
            User customer = cs.loginN(name, password);

            if (customer != null) {
                // 登录成功，弹出新功能菜单
                new CustomerFrame().CustomerFrame(customer);
                loginFrameN.dispose();
            }
        });


        //商家登录
        loginButtonM.addActionListener(e -> {
            String name = nameField.getText();
            String password = passwordField.getText();

            MerchantService ms = new MerchantService();
            User merchant = ms.loginN(name, password);

            if (merchant != null) {
                // 登录成功，弹出新功能菜单
                new MerchantFrame().MerchantFrame(merchant);
                loginFrameN.dispose();
            }
        });

        //生产商登录
        loginButtonP.addActionListener(e -> {
            String name = nameField.getText();
            String password = passwordField.getText();

            ProducerService ps = new ProducerService();
            User producer = ps.loginN(name, password);

            if (producer != null) {
                // 登录成功，弹出新功能菜单
                new ProducerFrame().ProducerFrame(producer);
                loginFrameN.dispose();
            }
        });



        returnButton.addActionListener(e -> {
            loginFrameN.dispose();
            new Menu().Menu();
        });
        
        
        
    }
    
    

}
